using SqlSugar;
using IM.Easy.Core.Services;
using IM.Easy.Core.Utils;
using IM.Easy.Infrastructure.Query;
using IM.Easy.Core.Extensions;
using IM.Easy.Entity.System;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using Mapster;

namespace IM.Easy.SystemServices.Services
{
    public class ShortcutService : AppServices<Shortcut>, IShortcutService
    {

        public ShortcutService(ISqlSugarClient db) : base(db)
        {

        }

        public Shortcut Create(Shortcut entity)
        {
            var exist = base.IsAny(s => s.Owner == entity.Owner && entity.MenuId == s.MenuId);
            if (exist) { ExceptionUtils.ThrowBusinessException("该快捷菜单已存在"); }
            // TODO: 添加创建逻辑
            return Context.Insertable(entity).ExecuteReturnEntity();
        }

        public Shortcut GetById(long id)
        {
            // TODO: 添加获取逻辑
            return Context.Queryable<Shortcut>().InSingle(id);
        }

        public PageResponse<Shortcut> GetPageList(ShortcutQueryDto query)
        {
            var queryable = Context.Queryable<Shortcut>();
            var exp = Expressionable.Create<Shortcut>();
            exp = exp
;
            var page = queryable
                 .Where(exp.ToExpression())
                 .ToPageList<Shortcut, Shortcut>(query);
            return page;
        }


        /// <summary>
        /// 获取用户快捷入口
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<ShortcutDto> GetUserShortcuts(long userId)
        {
            var shortcuts = base.GetList(s => s.Owner == userId);
            return shortcuts.Adapt<List<ShortcutDto>>();
        }

        public bool Update(Shortcut entity)
        {
            // TODO: 添加更新逻辑
            return Context.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params long[] MenuId)
        {
            // TODO: 添加删除逻辑
            return Context.Deleteable<Shortcut>(MenuId).ExecuteCommandHasChange();
        }
    }
}
